
var table;
//相关业务逻辑
$(function(){
	orderProcedure.bind();
	orderProcedure.getOrderProcedure();
});


var orderProcedure={//页面总变量
		userId:"",
		searchWhere:'',
		bind:function(){
//			$("#addTemplate").bind("click",function(){
//				$("input[type='checkbox']").prop("checked",false); 
//				$("#addTemplateModal").modal("show");
//			});
//			
			$("#findUser").bind("click",function(){
				orderProcedure.getOrderProcedure();
			});
			
			
			$("#processSerachBtn").bind("click",function(){
				orderProcedure.searchWhere=$("#processSerachText").val();
				orderProcedure.getAllWorkShopAndProcedure();
			});
			
			$("#addModalBtn").bind("click",function(){
				var procedureArray=new Array();
				
				
				var treeObj = $.fn.zTree.getZTreeObj("tree");
				var nodes = treeObj.getCheckedNodes(true);
				nodes=_.filter(nodes,{"isParent":false});
				console.log(nodes);
				var procedureArray =new Array();
				for(var key in nodes){
					var pro=new Object();
					pro.workshopName=nodes[key].pId;
					pro.processName=nodes[key].id;
					procedureArray.push(pro);
				}
				if(procedureArray.length<=0){
					layAlert("请选择工序");
					return false;
				}
                ajaxUtil.ajaxRuquest("orderProcedureTemplate/addUserProcedure",{"procedureArray":JSON.stringify(procedureArray),"id":orderProcedure.userId},function(data){
					layer.msg(data,{icon:1});
					table.ajax.reload( null, false);
					$("#addTemplateModal").modal("hide");
				});
			});
			
			$('#orderProcedureTable tbody').on('click', '.update', function () {
				orderProcedure.userId=$(this).val();
				orderProcedure.getAllWorkShopAndProcedure();
				$("#addTemplateModal").modal("show");
			});
			
			//点击移除按钮
			$('#orderProcedureTable tbody').on('click', '.remove', function () {
				var text=$(this).text();
				if(text=="移除"){
					$(this).parents().children('td').children('.deleteX').show();
					$(this).text("完成");
				}else{
					$(this).parents().children('td').children('.deleteX').hide();
					$(this).text("移除");
				}
			});
			
			//点击x
			$('#orderProcedureTable tbody').on('click', '.deleteX', function () {
				var rowIdex= $(this).parent()[0]._DT_CellIndex.row; 
				var userId=$(this).parents().children('td').children('.remove').val();
				var procedureId=$(this).attr("value");
				ajaxUtil.ajaxRuquest("orderProcedureTemplate/removeUserProcedure",{"userId":userId,"procedureId":procedureId},function(data){
					layer.msg(data,{icon:1});
				    table.ajax.reload(function(data){
				    	table.rows().nodes().flatten().to$().eq(rowIdex).children('td').children('.deleteX').show();
				    	table.rows().nodes().flatten().to$().eq(rowIdex).children('td').children('.remove').text("完成");
				    }, false);
				});
			});
		},
		checkBind:function(){
			$(".workShopCheck").bind("click",function(){
				var name=$(this).attr("name");
				var is=$(this).is(":checked");
				if(is){
					$("input[name='proce"+name+"']").prop("checked",true); 
				}else{
					$("input[name='proce"+name+"']").prop("checked",false); 
				}
			});
			
			$(".proceCheck").bind("click",function(){
				var name=$(this).attr("name");
				var fatherName=name.substring(5,name.length);
				var length=$("input[name='"+name+"']").length; 
				var checkLength=$("input[name='"+name+"']:checked").length; 
				if(length==checkLength){
					$("input[name='"+fatherName+"']").prop("checked",true); 
				}else{
					$("input[name='"+fatherName+"']").prop("checked",false)
				}
			});
		},
		getOrderProcedure:function(){
			var userName=$("#inputUserName").val();
			table=ajaxUtil.dataTablesAjaxNotFxiaction("orderProcedureTable",10,"orderProcedureTemplate/getUserProcedure",
					{"userName":userName},[
					{"data":"name"},
					{"data":"workShopList",render: function (data){
						var html="";
						for(var i=0;i<data.length;i++){
							var processList=data[i].processList;
							if(i>0){
								html+="<br/><br/>"
							}
							html+="<button class='btn btn-warning radius pad'>"+data[i].workshopName+"</button> &nbsp;"
							for(var j=0;j<processList.length;j++){
								html+="<button class='btn btn-success radius pad'>"+processList[j].processName+"</button><i class='deleteX Hui-iconfont' hidden='hidden' value='"+processList[j].id+"'>&#xe60b;</i>&nbsp;"
							}
						}
						return html;
					}},
					{"data":"id",render: function (data) {
//						return "<button class='update btn btn-secondary radius ' type='button' value='"+data+"' >修改</button> " +
							return	"<button value='"+data+"'  class='update btn btn-secondary radius '>添加</button> &nbsp;"+
							    	"<button value='"+data+"'  class='remove btn btn-success radius '>移除</button";
					}}
			]);
		},
		//获取所有车间下的所有工序
		getAllWorkShopAndProcedure:function(){
			$('.workShopCheck').unbind("click"); 
			$('.proceCheck').unbind("click"); 
			var setting = {
					view: {
						showIcon: false
					},
					check: {
						enable: true
					},
					data : {
						simpleData : {
							enable : true
						}
					}
			};
			ajaxUtil.ajaxRuquest("orderProcedureTemplate/getAllWorkShopAndProcedure",{"type":1,"userId":orderProcedure.userId,"fuzzy":orderProcedure.searchWhere},function(data){
				var xx=orderProcedure.getZTreeData(data);
				xx=_.filter(xx,function(x){return x.name=='车房开发'});
				$.fn.zTree.init($("#tree"), setting, orderProcedure.getZTreeData(data));
				
//				var html="";
//				for(var i=0;i<data.length;i++){
//					var proceList=data[i].processList;
//					html+="<input name='"+i+"' type='checkbox' class='workShopCheck' value='"+data[i].id+"'>"+data[i].workshopName+"</br>";
//					for(var j=0;j<proceList.length;j++){
//						html+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name='proce"+i+"'type='checkbox' value="+proceList[j].id+" class='proceCheck'>"+proceList[j].processName+"<br/>";
//					}
//				}
//				$("#checkProce").html("");
//				$("#checkProce").append(html);
//				orderProcedure.checkBind();
			});
		},
		getZTreeData:function(data){
			if(data==null || data.length==0){
				return  new Array();
			}
			var array=new Array();
			for(var i=0;i<data.length;i++){
			    var object=new Object();
			    object.id=data[i].id;
			    object.uniqId=data[i].id+'work';
			    object.pId=null;
			    object.name=data[i].workshopName;
			    array.push(object);
				var processList=data[i].processList;
			    for(var j=0;j<processList.length;j++){
			    	var object1=new Object();
			    	object1.id=processList[j].id;
			    	object1.uniqId=processList[j].id+'pro';
			    	object1.pId=data[i].id;
			    	object1.name=processList[j].processName;
				    array.push(object1);
			    }
			}
			array=_.uniqBy(array,"uniqId");
			return array;
		}
}

//setInterval ("table.ajax.reload(null, false)", 1000);//十五分钟更新一次数据
